package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DepartmentMapper;
import com.kkb.mapper.ExtralHosRegisterMapper;
import com.kkb.mapper.HosRegisterMapper;
import com.kkb.mapper.HospitalizationMapper;
import com.kkb.pojo.*;
import com.kkb.vo.HostRegisterQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class HosRegisterService {
    @Resource
    HosRegisterMapper mapper;

    @Resource
    ExtralHosRegisterMapper extralHosRegisterMapper;

    @Resource
    private HospitalizationMapper hospitalizationMapper;

    @Resource
    DepartmentMapper departmentMapper;

    public HosRegister queryById(int id) {
        HosRegister hosRegister = mapper.selectByPrimaryKey(id);
        Hospitalization hospitalization = hospitalizationMapper.selectByPatientId(hosRegister.getId());
        hosRegister.setHospitalization(hospitalization);
        return hosRegister;
    }

    public List queryByIdList(List ids){
        return mapper.selectByIdList(ids);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<HosRegister> selectInPage(Integer index, Integer size, HostRegisterQueryVO hostRegisterQueryVO){
        if (hostRegisterQueryVO.getDoctorName()==""){
            hostRegisterQueryVO.setDoctorName(null);
        }else {
            String doctorName = hostRegisterQueryVO.getDoctorName();
            hostRegisterQueryVO.setDoctorName("%"+doctorName+"%");
        }
        if (hostRegisterQueryVO.getDepartmentName()==""){
            hostRegisterQueryVO.setDepartmentName(null);
        }else {
            String departmentName = hostRegisterQueryVO.getDepartmentName();
            hostRegisterQueryVO.setDepartmentName("%"+departmentName+"%");
        }
        PageHelper.startPage(index,size);
        List<HosRegister> hosRegisters = extralHosRegisterMapper.selectInPage(hostRegisterQueryVO);
        for (HosRegister h:hosRegisters) {
            Hospitalization hospitalization = hospitalizationMapper.selectByPatientId(h.getId());
            h.setHospitalization(hospitalization);
        }
        return new PageInfo<HosRegister>(hosRegisters);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int quitState(Integer id){
        HosRegister hosRegister = new HosRegister();
        hosRegister.setId(id);
        hosRegister.setState(3);
        return mapper.updateByPrimaryKeySelective(hosRegister);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateHosRegister(HosRegister hosRegister, Integer doctorId){
        hosRegister.setDoctorId(doctorId);
        return mapper.updateByPrimaryKeySelective(hosRegister);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int insertRegister(HosRegister hosRegister){
        if (hosRegister==null){
            return 0;
        }
        hosRegister.setRegDate(new Date());
        hosRegister.setState(0);
        return mapper.insertSelective(hosRegister);
    }
}
